<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/30
 * Time: 10:49
 */
namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\User as UserModel;
use app\api\service\TokenService;
use app\lib\exception\NotFoundException;
use app\lib\exception\SuccessMessage;

class Address extends BaseController
{


    protected $beforeActionList = [
        'checkPrimaryScope'
    ];

    public function CreateOrUpdateAddress(){
        $validate = new \app\api\validate\Address();
        $validate->goCheck();
        $uid = TokenService::getCurrentUid();

        $user = UserModel::find($uid);
        if(!$user){
            throw new NotFoundException(['msg'=>'用户不存在']);
        }
        $data = $validate->getDataByRule(input('post.'));
        $userAddress = $user->address;
        if(!$userAddress){
            $user->address()->save($data);
        }else{
            $user->address->save($data);
        }

        return (new SuccessMessage());

    }


}